/**
 * MCP test setup
 * Specialized setup for MCP (Model Context Protocol) tests
 */

// Set test environment variables
process.env.NODE_ENV = 'test';
process.env.VA_LOG_LEVEL = 'error'; // Reduce log noise during tests

// Load test utilities
const TestUtils = require('../setup');

// Extend test utils for MCP-specific functionality
Object.assign(global.testUtils, {
  /**
   * Create a mock MCP request
   * @param {Object} options - Request options
   * @returns {Object} Mock MCP request
   */
  createMockMCPRequest: (options = {}) => ({
    id: options.id || 'test-request-1',
    method: options.method || 'tools/call',
    params: {
      name: options.toolName || 'analyze_image',
      arguments: options.arguments || {}
    },
    ...options
  }),

  /**
   * Create a mock MCP response
   * @param {Object} options - Response options
   * @returns {Object} Mock MCP response
   */
  createMockMCPResponse: (options = {}) => ({
    id: options.id || 'test-response-1',
    result: options.result || null,
    error: options.error || null,
    ...options
  }),

  /**
   * Create a mock image file path for testing
   * @param {string} filename - Image filename
   * @returns {string} Mock image path
   */
  createMockImagePath: (filename = 'test-image.png') => {
    const path = require('path');
    return path.join(__dirname, '..', 'fixtures', 'images', filename);
  },

  /**
   * Create a temporary MCP configuration for testing
   * @param {Object} configOverrides - Configuration overrides
   * @returns {Object} Temporary MCP configuration
   */
  createTempMCPConfig: (configOverrides = {}) => {
    return {
      server_name: 'vision-analyzer-test',
      server_version: '1.0.0-test',
      timeout: 5000,
      features: {
        auto_model_selection: true,
        cache_analysis: false, // Disable caching for tests
        debug_mode: true
      },
      ...configOverrides
    };
  },

  /**
   * Wait for async MCP operations
   * @param {number} ms - Milliseconds to wait
   * @returns {Promise} Promise that resolves after timeout
   */
  waitForMCP: (ms = 100) => new Promise(resolve => setTimeout(resolve, ms)),

  /**
   * Clean up MCP test resources
   */
  cleanupMCP: () => {
    // Clean up any temporary files or connections
    // This will be called after each MCP test
  }
});

// Global test hooks
beforeEach(() => {
  // Reset any global state before each MCP test
  jest.clearAllMocks();
});

afterEach(() => {
  // Clean up after each MCP test
  global.testUtils.cleanupMCP();
});

// Export for use in test files
module.exports = {
  createMockMCPRequest: global.testUtils.createMockMCPRequest,
  createMockMCPResponse: global.testUtils.createMockMCPResponse,
  createMockImagePath: global.testUtils.createMockImagePath,
  createTempMCPConfig: global.testUtils.createTempMCPConfig,
  waitForMCP: global.testUtils.waitForMCP
};